var uid, doctorid, doctorname, doctoravatar, tcall, pcall, vcall, videoprice, money;
var ppv = '';//图文，电话， 视频
var ispictext, isvideo;//医生是否开启了图文聊天或视频聊天
var balance;	//余额支付
document.addEventListener('plusready',function(){
	uid = plus.storage.getItem('uid');
	balance = plus.storage.getItem('money');
	var ws = plus.webview.currentWebview();
	var aid = ws.aid;//医生Id
	console.log(aid+" "+balance);
	var dd = plus.storage.getItem('dlist');
	var dlist = $.parseJSON(dd);
	$.each(dlist, function(k, v) {
		if(v.id == aid){
			console.log(JSON.stringify(v));
			doctorid = v.id;
			doctorname = v.name;
			doctoravatar = v.avatar;
			$('.name').text(v.name);
			$('.resign').text(v.resign);
			$('.chief').text(v.chief);
			$('.surgery').append(v.surgery);
			$('.avatar').attr('src',getAvatar(v.avatar));
			$('.doctor-text').html(v.experience);
			if(v.phone){
				$('.phone-li').attr('value',v.phone);
			}else{
				$('.phone-li').attr('value',v.user);
			}
		}
	});
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=studio&a=allPrice&did="+aid,
		success :function(data){
			plus.storage.setItem('ndat', data.zixun); 
			var data1 = $.parseJSON(data);
			var dt = data1.zixun;
//			console.log(JSON.stringify(data1));
			ispictext = data1.dinfo.online;
			if(ispictext != '1'){		//该医生未开启图文咨询
				$('#phone').removeAttr("id"); 
				$('#text').removeAttr("id"); 
			}
			isvideo = data1.dinfo.video;
			if(isvideo != '1'){		//该医生未开启视频咨询
				$('#video').removeAttr("id"); 
			}
			if(dt[0]){
				$('.textbox').text(dt[0]);
				tcall = dt[0];
			}else{
				$('.tex').text('期待开通');
				$('.pictext').removeClass('color-yellowish');
				$('.pictext').addClass('color-ddd');
				$('#text').removeAttr("id"); 
			}
			if(dt[1]){
				$('.telcall').text(dt[1]);
				pcall = dt[1];
			}else{
				$('.pho').text('期待开通');
				$('.tel').removeClass('color-yellowish');
				$('.tel').addClass('color-ddd');
				$('#phone').removeAttr("id"); 
			}
			if(dt[2]){
				videoprice = dt[2];
				$('.video').text(dt[2]);
			}else{
				$('.video').text('期待开通');
				$('.shipin').removeClass('color-yellowish');
				$('.shipin').addClass('color-ddd');
				$('#video').removeAttr("id"); 
			}
			plus.nativeUI.closeWaiting();
		},
		error :function(e){
			console.log(JSON.stringify(e));
			var dat = plus.storage.getItem('ndat');
			var dta = $.parseJSON(dat);
			$('.textbox').text(dta.textbox);
			$('.telcall').text(dta.telcall);
			$('.video').text(dta.video);
		}
	});
	//点击电话咨询
	$('.tel').on('tap', function(){
		if(ispictext != '1'){
			toast('该医生暂未开启电话咨询咨询');
		}else{
			ppv = 2;//电话咨询
			money = pcall;
		}
	})
	//点击图文咨询
	$('.pictext').on('tap', function(){
		if(ispictext != '1'){
			toast('该医生暂未开启图文咨询咨询');
		}else{
			ppv = '';//图文咨询
			money = tcall;
		}
	})
	//点击视频咨询
	$('.shipin').on('tap', function(){
		if(isvideo != '1'){
			toast('该医生暂未开启视频咨询');
		}
	})
	//支付方式
	$('.zhiftc div').on('tap', function(){
		switch($(this).index()){
			case 1://点击微信支付
			ptype = 'wxpay'; 
			payment(money);
			break;
			case 2://点击支付宝支付
			ptype = 'alipay'; 
			payment(money);
			break;
			case 3://点击银联支付
			toast('待接口提供');return;
			break;
			case 4://点击余额支付
			console.log(money+ " "+balance);
			if(parseFloat(balance) < parseFloat(money)){
				toast('您的余额不足请及时充值');
			}else{
				ReBalance();
			}
			break;
		}
	})
    //立即预约
    $(".video-li").on('tap',function(){
    	goNewPage('find-doctor-time-video-ing.html',{doctorid:doctorid,doctorname:doctorname,doctoravatar:doctoravatar, videoprice:videoprice})
	});
	//选择预约时间
	$(".video-yuyue").on('tap',function(){
		goNewPage('find-doctor-time-video.html', {doctorid:doctorid,doctorname:doctorname,doctoravatar:doctoravatar, videoprice:videoprice});
	})
},false)
var p;
//添加收入支出记录
function source(){
	if(ppv == 2){
		p = '电话咨询';
	}else{
		p = '图文咨询';
	}
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=income&a=recharge",
		data :{
			payer :uid,
			earner :doctorid,
			source :p,
			money :money,
			tid :77//支出
		},
		dataType :'json',
		success :function(data){
			console.log(JSON.stringify(data));
			if(data > 0){
				if(ppv == '2'){
					payOrder();
				}else{
					goNewPage('home-chat.html',{doctorid:doctorid});
				}
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
//减少账户余额
function ReBalance(){
	$.ajax({
		type:"get",
		url:apiRoot+"/home/income/Decbalance",
		data :{
			uid :uid,
			money :money
		},
		success:function(data){
			console.log(data);
			var str = parseFloat(balance) - parseFloat(money);
			plus.storage.setItem('money', str+'');
			source();
		},
		error :function(e){
			console.log(JSON.stringify(e));
		}
	});
}
//打电话
function payOrder(){
	var telNum = $('.phone-li').attr('value');
	console.log(telNum);
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=chat&a=sendMessage",
		data :{
			sendid :uid,
			receiveid :doctorid,
			type :ppv,
			tid :76
		},
		dataType :'json',
		success :function(data){
			console.log(JSON.stringify(data));
			if(data > 0){
				plus.device.dial(telNum,false);
			}
		},
		error :function(e){
			errortoast();
		}
	});
}
